package com.insigma.business.qrcode.soldier.service.impl;


import com.insigma.business.qrcode.common.converter.SoldierBasicInfoProviderConverter;
import com.insigma.business.qrcode.common._provider.CommonAdminAreaProvider;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.PdfUtils;
import com.insigma.business.qrcode.soldier.dto.ElectronicLicense;
import com.insigma.business.qrcode.soldier.dto.SoldierBasicInfoDTO;
import com.insigma.business.qrcode.soldier.entity.InsuredInfoEntity;
import com.insigma.business.qrcode.soldier.entity.SoldierBasicInfoDO;
import com.insigma.business.qrcode.soldier.entity.SoldierHouseholdRegistrationInfoDO;
import com.insigma.business.qrcode.soldier.service.SoldierBasicInfoService;
import com.insigma.business.qrcode.soldier.service.ThirdPartyService;
import com.insigma.business.qrcode.soldier.util.ThreadPoolManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Callable;


@Slf4j
@Service("soldierBasicRefresher")
public class SoldierBasicRefresher {

    @Value("${sm.tmp.dir}")
    private String tmpDir;

    @Autowired
    private SoldierBasicInfoService soldierBasicInfoService;

    @Autowired
    private ThirdPartyService thirdPartyService;

    @Autowired
    private CommonAdminAreaProvider commonAdminAreaProvider;


    public String refreshAllBasicInfo(String idCardNo) {

        Boolean changed = false;
        SoldierBasicInfoDO basicInfoDO = soldierBasicInfoService.getByIdCardNo(idCardNo);
        BizCheckUtil.checkNotNull(basicInfoDO, "信息错误");
        SoldierHouseholdRegistrationInfoDO registrationInfoDO = thirdPartyService.queryHouseholdRegistrationInfo(basicInfoDO.getIdCardNo(), basicInfoDO.getRealName());

        if (Strings.isBlank(basicInfoDO.getNation())) {
            if (registrationInfoDO != null) {
                basicInfoDO.setNation(registrationInfoDO.getMzmc());
                changed = true;
            }
        }

        if (Strings.isBlank(basicInfoDO.getRootAdminAreaValue())) {
            if (registrationInfoDO != null) {
                basicInfoDO.setRootAdminAreaValue(registrationInfoDO.getHjdz());
                basicInfoDO.setGaRootAdminAreaValue(registrationInfoDO.getHjdz());
                SoldierBasicInfoDTO soldierBasicInfoDTO = SoldierBasicInfoProviderConverter.convertResponseDO2DTO(basicInfoDO);
                String adminCode = commonAdminAreaProvider.analyseAreaCodeByBasicInfo(soldierBasicInfoDTO).getDto();
                basicInfoDO.setRootAdminAreaCode(adminCode);
                basicInfoDO.setGaRootAdminAreaCode(adminCode);
                changed = true;
            }
        }

        if (Strings.isBlank(basicInfoDO.getMaritalStatus())) {
            if (registrationInfoDO != null) {
                basicInfoDO.setMaritalStatus(registrationInfoDO.getHyzkmc());
                changed = true;
            }
        }

        if (Strings.isBlank(basicInfoDO.getNowWorkUnit())) {
            List<InsuredInfoEntity> insuredInfoEntities = thirdPartyService.queryInusreList(basicInfoDO.getIdCardNo(), basicInfoDO.getRealName());
            if (CollectionUtils.isNotEmpty(insuredInfoEntities)) {
                String aab004 = insuredInfoEntities.get(0).getAab004();
                basicInfoDO.setNowWorkUnit(aab004);
                changed = true;
            }
        }

        if (Strings.isBlank(basicInfoDO.getAvatarPath())) {
            ElectronicLicense electronicLicense = thirdPartyService.queryElectronicLicense(idCardNo);
            if (electronicLicense != null) {
                String fileUrl = electronicLicense.getFileurl();
                if (fileUrl != null) {
                    String ossAvatarPath = PdfUtils.extractImageFromPathToOss(fileUrl, idCardNo, tmpDir);
                    basicInfoDO.setAvatarPath(ossAvatarPath);
                    basicInfoDO.setAvatarPathUp(ossAvatarPath);
                    changed = true;
                }
            }
        }

        if (changed) {
            soldierBasicInfoService.update(basicInfoDO);
        }
        return idCardNo;
    }

    public void asyncRefreshAllBasicInfo(String idCardNo) {
        ThreadPoolManager.getInstance().submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    return refreshAllBasicInfo(idCardNo);
                } catch (Exception e) {
                    log.error("执行异常：{}", e);
                }
                return null;
            }
        });
    }

}
